//
//  HLAudioView.m
//  YueYue
//
//  Created by hulu001 on 2023/6/12.
//

#import "HLAudioView.h"

@interface HLAudioContentView : UIView
@property (strong,  nonatomic) UIImageView *imageView;
@property (strong,  nonatomic) UILabel *textLabel;
@property (assign,  nonatomic) NSInteger status; // 1:录音中 2:取消
@end

@implementation HLAudioContentView
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 12;
        self.layer.masksToBounds = YES;
        
        self.imageView = [UIImageView.alloc init];
        [self addSubview:self.imageView];
        
        self.textLabel = [UILabel.alloc init];
        self.textLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.textLabel];
        
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(48, 48));
            make.top.mas_equalTo(24);
            make.centerX.equalTo(self.mas_centerX);
        }];
        [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_centerX);
            make.top.equalTo(self.imageView.mas_bottom).offset(12);
        }];
    }
    return self;
}

- (void)setStatus:(NSInteger)status
{
    _status = status;
    
    if (status == 1) {
        self.imageView.image = [UIImage animatedImageWithImages:@[[UIImage imageNamed:@"imRecording0"],
                                           [UIImage imageNamed:@"imRecording1"],
                                           [UIImage imageNamed:@"imRecording2"]] duration:0.5];
        self.textLabel.text = @"手指上滑 取消发送";
        self.textLabel.textColor = HLTextColor1;
    } else {
        self.imageView.image = [UIImage imageNamed:@"imAudioCancel"];
        self.textLabel.text = @"松开手指 取消发送";
        self.textLabel.textColor = HLTextColor1;
    }
}

@end

@interface HLAudioView ()

@property (nonatomic, strong) HLAudioContentView *contentView;

@end

@implementation HLAudioView


+ (HLAudioView*)sharedView {
    static dispatch_once_t once;
    
    static HLAudioView *sharedView;
#if !defined(SV_APP_EXTENSIONS)
    dispatch_once(&once, ^{ sharedView = [[self alloc] initWithFrame:[[[UIApplication sharedApplication] delegate] window].bounds]; });
#else
    dispatch_once(&once, ^{ sharedView = [[self alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; });
#endif
    return sharedView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        [self addSubview:self.contentView];
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(160, 120));
            make.centerX.equalTo(self.mas_centerX);
            make.centerY.equalTo(self.mas_centerY).offset(-50);
        }];
    }
    return self;
}

+ (HLAudioView *)showRecordingView
{
    HLAudioView *view = [self sharedView];
    if (![self sharedView].superview) {
        [ HLUIHelper.keyWindow addSubview:view];
    }
    view.contentView.status = 1;
    view.isRecording = YES;
    
    return view;
}

+ (HLAudioView *)showCancelView
{
    HLAudioView *view = [self sharedView];
    if (![self sharedView].superview) {
        [ HLUIHelper.keyWindow addSubview:view];
    }
    view.contentView.status = 2;
    
    return [self sharedView];
}

+ (void)dismiss
{
    [self sharedView].isRecording = NO;
    [[self sharedView] removeFromSuperview];
    for (UIView *sub in  HLUIHelper.keyWindow.subviews) {
        if ([sub isKindOfClass:HLAudioView.class]) {
            [sub removeFromSuperview];
            break;
        }
    }
}
+ (HLAudioView *)HUD
{
    HLAudioView *view = nil;
    for (UIView *sub in HLUIHelper.keyWindow.subviews) {
        if ([sub isKindOfClass:HLAudioView.class]) {
            view = (HLAudioView *)sub;
            break;
        }
    }
    
    return view;
}

#pragma mark - lazy

- (HLAudioContentView *)contentView
{
    if (!_contentView) {
        _contentView = [HLAudioContentView.alloc init];
        _contentView.status = 1;
    }
    return _contentView;
}


@end
